trigger trg_VisaHistory on Visa_History__c (after insert, before update)
{

	//This Map Will Hold (AccountId , Visa Id)
    Map<Id,Visa_History__c> visaObjectMap = new Map<Id,Visa_History__c>();
    
    Map<Id,Visa_History__c> visaObjectInsertMap = new Map<Id,Visa_History__c>();
    
    //Get all Accounts which will be updated with Current Visa Id
    if(Trigger.isInsert)
    {
    	for(Visa_History__c visaObject : Trigger.new)
	    {
	    	if(visaObject.isCurrent__c == true)
            {
            	System.debug('Initial => '+ visaObject.Id);
                visaObjectInsertMap.put(visaObject.Visa_Holder__c , visaObject);  
                  
            }
	    }
    	
    }
    else if(Trigger.isUpdate)
    {
    	for(Visa_History__c visaObject : Trigger.new)
	    {
	        if(Trigger.oldmap.get(visaObject.id).iscurrent__c != visaObject.isCurrent__c)
	        {
	            if(visaObject.isCurrent__c == true)
	            {
	                visaObjectMap.put(visaObject.Visa_Holder__c , visaObject);  
	            }
	        }
	        
	    }	
    	
    }
    
	//For Inserting Purpose...
    if(visaObjectInsertMap.size() > 0)
    {
    	//Get old Passport which is isCurrent = true and mark it as false
		List<Visa_History__c> oldVisaList = new List<Visa_History__c>();
		for(Visa_History__c visaObject : [select isCurrent__c,Visa_Holder__c from Visa_History__c where Visa_Holder__c IN : visaObjectInsertMap.keyset() and isCurrent__c =: true ])
		{
			if(visaObject.Id != visaObjectInsertMap.get(visaObject.Visa_Holder__c).id)
			{
				System.debug('After => '+ visaObject.Id);
				visaObject.isCurrent__c = false ;
				//Adding in the List...
				oldVisaList.add(visaObject);
			}
		}
                
        //Query all Accounts with Selected Ids and Update Current Visa
        //List<Account> accountList = new List<Account>([select Visa_History_Number__c from Account where id in : accountIds]);
        List<Contact> contactList = new List<Contact>([select Visa_Reference_Number__c from Contact where id in : visaObjectInsertMap.keyset()]);
        for (Contact conObject : contactList)
        {
        	System.debug('In Contact Field... => '+ visaObjectInsertMap.get(conObject.id));	
           	conObject.Visa_Reference_Number__c = visaObjectInsertMap.get(conObject.id).id;
           	conObject.Current_Employer__c = visaObjectInsertMap.get(conObject.id).Sponsor__c;
        }
        
        if(oldVisaList.size() > 0)
        	update oldVisaList;
        	
        if (contactList.size() > 0 )
           update contactList;
        
    }
    
	//For Update Purpose....
    if(visaObjectMap.size()> 0)
    {
		
		//Get old Passport which is isCurrent = true and mark it as false
		List<Visa_History__c> oldVisaList = new List<Visa_History__c>();
		for(Visa_History__c visaObject : [select isCurrent__c from Visa_History__c where Visa_Holder__c IN : visaObjectMap.keyset() and isCurrent__c =: true ])
		{
			visaObject.isCurrent__c = false ;
			//Adding in the List...
			oldVisaList.add(visaObject);
		}
                
        //Query all Accounts with Selected Ids and Update Current Visa
        List<Contact> contactList = new List<Contact>([select Visa_Reference_Number__c from Contact where id in : visaObjectMap.keyset()]);
        for (Contact conObject : contactList){
           conObject.Visa_Reference_Number__c = visaObjectMap.get(conObject.id).Id;
           conObject.Current_Employer__c = visaObjectMap.get(conObject.id).Sponsor__c;
        }
        
        if(oldVisaList.size() > 0)
        	update oldVisaList;
        	
        if (contactList.size() > 0 )
           update contactList;
        
    }        
	



}